package com.sixth.controller;

import com.sixth.bean.bo.CategoryCreateBO;
import com.sixth.bean.bo.CategoryUpdateBO;
import com.sixth.bean.common.BaseRespVo;
import com.sixth.bean.vo.*;
import com.sixth.service.CategoryService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 商品类目模块的controller
 * @Author lixinhang
 * @Time 2022/9/6 23:23
 */
@RestController
@RequestMapping("admin/category")
public class CategoryController {

    @Autowired
    CategoryService categoryService;

    //查询全部类目的方法
    @RequiresPermissions(value = {"admin:category:list"},logical = Logical.OR)
    @RequestMapping("list")
    public BaseRespVo categoryList(){
        CategoryListVO categoryListVO =  categoryService.categoryList();
        return BaseRespVo.ok(categoryListVO);

    }

    //检索全部的一级目录
//    @RequiresPermissions(value = {"admin:category:l1"},logical = Logical.OR)
    @RequestMapping("l1")
    public BaseRespVo categoryL1(){
        CategoryL1VO categoryL1VO = categoryService.categoryL1();
        return BaseRespVo.ok(categoryL1VO);
    }

    //新建类目
    @RequiresPermissions(value = {"admin:category:create"},logical = Logical.OR)
    @RequestMapping("create")
    public BaseRespVo categoryCreate(@RequestBody CategoryCreateBO categoryCreateBO){
        CategoryCreateVO categoryCreateVO = categoryService.categoryCreate(categoryCreateBO);
        return BaseRespVo.ok(categoryCreateVO);
    }

    //更新类目
    @RequiresPermissions(value = {"admin:category:update"},logical = Logical.OR)
    @RequestMapping("update")
    public BaseRespVo categoryUpdate(@RequestBody CategoryUpdateBO categoryUpdateBO){
        if("L2".equals(categoryUpdateBO.getLevel()) && categoryUpdateBO.getPid() == null){
            return BaseRespVo.error("请指定父级目录");
        }
        categoryService.categoryUpdate(categoryUpdateBO);
        return BaseRespVo.ok();
    }

    //删除所选类目的方法
    @RequiresPermissions(value = {"admin:category:delete"},logical = Logical.OR)
    @RequestMapping("delete")
    public BaseRespVo categoryDelete(@RequestBody InnerCategoriesOfCategoryListVO categoryDeleteBO){
        categoryService.categoryDelete(categoryDeleteBO);
        return BaseRespVo.ok();
    }

}
